package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.SmCourseware;
import com.shycloud.mido.member.api.entity.SmCoursewareFolder;
import com.shycloud.mido.member.api.entity.SmCoursewareGroup;
import com.shycloud.mido.member.api.entity.SmCoursewareTemp;
import com.shycloud.mido.member.api.vo.SmCoursewareFolderVO;
import com.shycloud.mido.member.api.vo.SmCoursewareTempVO;
import com.shycloud.mido.member.mapper.SmCoursewareTempMapper;
import com.shycloud.mido.member.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 课件业务 controller
 *
 * @author nianhua.jiang
 * @date 2020/10/09
 */
@RestController
@AllArgsConstructor
@RequestMapping("/courseware")
@ApiOperation("课件业务模块")
@ApiIgnore
public class CoursewareController {

	/** 课件分组模块 service */
	private SmCoursewareGroupService groupService;
	/** 文件夹模块 service */
	private SmCoursewareFolderService folderService;
	/** 课件文件模块 service */
	private SmCoursewareService coursewareService;
	/** 分组文件夹关系模块 service */
	private SmCoursewareGroupFolderService groupFolderService;
	/** 课件临时文件模块 service */
	private SmCoursewareTempService coursewareTempService;
	private SmCoursewareTempMapper coursewareTempMapper;

	/**
	 * 新增分组
	 *
	 * @param smCoursewareGroup
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/10/10 14:36
	 **/
	@PostMapping("saveGroup")
	public R saveGroup(@RequestBody SmCoursewareGroup smCoursewareGroup) {

		//用户ID
		smCoursewareGroup.setMemberId(SecurityUtils.getMember().getId());
		//新增场合判断分组名称是否重复
		if (smCoursewareGroup.getId() == null) {

			List<SmCoursewareGroup> nameList = groupService.list(new QueryWrapper<SmCoursewareGroup>()
					.eq(SmCoursewareGroup.COL_NAME, smCoursewareGroup.getName())
					.eq(SmCoursewareGroup.COL_MEMBER_ID, SecurityUtils.getMember().getId()));

			if (nameList.size() > 0) {
				return new R(CommonConstants.FAIL, "分组名称重复，请重新输入", false);
			}
		}

		return new R(groupService.saveOrUpdate(smCoursewareGroup));
	}

	/**
	 * 获取课件分组列表
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/10/10 14:52
	 **/
	@GetMapping("getGroupList")
	public R getGroupList() {
		return new R(groupService.getGroupList());
	}

	/**
	 * 删除分组
	 *
	 * @param id
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/10/10 16:43
	 **/
	@GetMapping("delGroup")
	public R delGroup(Integer id) {
		return new R(groupService.delGroup(id));
	}

	/**
	 * 添加文件夹
	 *
	 * @param smCoursewareFolderVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/10/12 14:42
	 **/
	@PostMapping("addFolder")
	public R addFolder(@RequestBody SmCoursewareFolderVO smCoursewareFolderVO) {
		return new R(CommonConstants.SUCCESS, "添加成功", folderService.addFolder(smCoursewareFolderVO));
	}

	/**
	 * 更新文件夹
	 *
	 * @param smCoursewareFolder
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/03 10:27
	 **/
	@PostMapping("updateFolder")
	public R updateFolder(@RequestBody SmCoursewareFolder smCoursewareFolder) {
		return new R(folderService.updateById(smCoursewareFolder));
	}

	/**
	 * 删除文件夹 (删除文件夹的同时将文件夹下的课件文件也删除)
	 *
	 * @param ids
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/10/12 17:25
	 **/
	@PostMapping("delFolder")
	public R delFolder(@RequestBody List<Integer> ids) {
		for (Integer id : ids) {
			folderService.delFolder(id);
		}
		return new R(true);
	}

	/**
	 * 添加/上传课件
	 *
	 * @param file
	 * @param type
	 * @param folderId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/10/10 10:07
	 **/
	@PostMapping("addCourseware")
	public R addCourseware(MultipartFile file, String type, Integer folderId, Integer musicId) {
		return new R(coursewareService.addCourseware(file, type, folderId, musicId));
	}

	@PostMapping("uploadFile")
	public R uploadFile(@RequestParam(value = "file") MultipartFile file) {
		return new R(coursewareService.uploadFile(file));
	}

	/**
	 * 获取课件列表
	 *
	 * @param folderId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/10/13 13:40
	 **/
	@GetMapping("getCoursewareList")
	public R getCoursewareList(Integer folderId, Integer current, Integer size) {
		return new R(coursewareService.getCoursewareList(new Page<>(current, size), folderId));
	}

	/**
	 * 删除课件
	 *
	 * @param map
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/10/13 15:10
	 **/
	@PostMapping("delCourseware")
	public R delCourseware(@RequestBody Map<String, Object> map) {
		Integer folderId = (Integer) map.get("folderId");
		List<Integer> ids = (List<Integer>) map.get("ids");
		return new R(coursewareService.delCourseware(folderId, ids));
	}

	/**
	 * 编辑课件
	 *
	 * @param courseware
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/10 15:04
	 **/
	@PostMapping("updCourseware")
	public R updCourseware(@RequestBody SmCourseware courseware) {

		if ("1".equals(courseware.getType())) {
			return new R(CommonConstants.FAIL, "曲目类型课件无法重命名", false);
		}
		return new R(coursewareService.updateById(courseware));
	}

	/**
	 * 获取课件文件夹列表
	 *
	 * @param groupId
	 * @param current
	 * @param size
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/2 17:20
	 **/
	@GetMapping("getFolderList")
	public R getFolderList(Integer groupId, Integer current, Integer size) {
		return new R(coursewareService.getFolderList(new Page<>(current, size), groupId));
	}

	/**
	 * 更新课件文件夹
	 *
	 * @param smCoursewareFolder
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/10 15:07
	 **/
	@GetMapping("updFolder")
	public R updFolder(SmCoursewareFolder smCoursewareFolder) {
		return new R(folderService.updateById(smCoursewareFolder));
	}

	/**
	 * 移动文件夹至其他分组
	 *
	 * @param map
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/3 13:12
	 **/
	@PostMapping("moveFolderToGroup")
	public R moveFolderToGroup(@RequestBody Map<String, Object> map) {

		//原分组ID
		Integer groupIdFrom = (Integer) map.get("groupIdFrom");
		//文件夹ID集合
		List<Integer> folderIds = (List<Integer>) map.get("folderIds");
		//目标分组ID
		Integer groupIdTo = (Integer) map.get("groupIdTo");
		return new R(groupFolderService.moveFolderToGroup(groupIdFrom, folderIds, groupIdTo));
	}

	/**
	 * 获取临时课件文件列表
	 *
	 * @param teacherId
	 * @param studentId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/20 10:03
	 **/
	@GetMapping("getCoursewareTemp")
	public R getCoursewareTemp(Integer current, Integer size, String teacherId, String studentId) {
		return new R(coursewareTempService.getCoursewareTemp(new Page<>(current, size), teacherId, studentId));
	}

	/**
	 * 获取临时课件文件列表
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/20 10:03
	 **/
	@GetMapping("getCoursewareTempExperience")
	public R getCoursewareTempExperience(Integer current, Integer size) {
		return new R(coursewareTempService.getCoursewareTemp(new Page<>(current, size), "-1", "-1"));
	}

	/**
	 * 添加临时课件
	 *
	 * @param smCoursewareTemp teacherId 教师 ID
	 * @param smCoursewareTemp studentId 学生 ID
	 * @param smCoursewareTemp type      类型： 1.曲目 2.视频 3.图片
	 * @param smCoursewareTemp musicId   曲目 ID
	 * @param smCoursewareTemp imgUrl    图片路径
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/20 10:28
	 **/
	@PostMapping("addCoursewareTemp")
	public R addCoursewareTemp(@RequestBody SmCoursewareTemp smCoursewareTemp) {

		smCoursewareTemp.setName("临时课件");

		if ("1".equals(smCoursewareTemp.getType())) {

			// 校验曲目重复
			List<SmCoursewareTemp> smCoursewareTemps = coursewareTempMapper.selectList(new QueryWrapper<SmCoursewareTemp>()
					.eq("teacher_id", smCoursewareTemp.getTeacherId())
					.eq("student_id", smCoursewareTemp.getStudentId())
					.eq("type", 1));

			for (SmCoursewareTemp coursewareTemp : smCoursewareTemps) {
				if (smCoursewareTemp.getMusicId().equals(coursewareTemp.getMusicId())) {
					throw new BusinessException("课件列表中已添加该曲目");
				}
			}

			String musicName = coursewareTempMapper.getMusicNameById(smCoursewareTemp.getMusicId());
			smCoursewareTemp.setName(musicName);
		}
		return new R(coursewareTempService.addCoursewareTemp(smCoursewareTemp));
	}

	/**
	 * 删除临时课件
	 *
	 * @param id
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/21 13:47
	 **/
	@GetMapping("delCoursewareTemp")
	public R delCoursewareTemp(Integer id) {
		return new R(coursewareTempService.delCoursewareTemp(id));
	}

	/**
	 * 修改临时课件
	 *
	 * @param smCoursewareTemp
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/11/25 10:07
	 **/
	@PostMapping("saveCourseTemp")
	public R saveCourseTemp(@RequestBody SmCoursewareTemp smCoursewareTemp) {
		return new R(coursewareTempService.updateById(smCoursewareTemp));
	}

	/**
	 * 批量上传课件文件图片
	 *
	 * @param imgArr
	 * @param coursewareTempVO
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/25 16:34
	 **/
	@PostMapping("uploadCoursewareTempImage")
	public R uploadCoursewareTempImage(@RequestParam("imgArr[]") List<MultipartFile> imgArr, SmCoursewareTempVO coursewareTempVO) {

		List<String> tempImageUrlList = new ArrayList<>();

		for (MultipartFile multipartFile : imgArr) {
			tempImageUrlList.add(coursewareService.uploadFile(multipartFile));
		}

		int i = 1;

		for (String tempImageUrl : tempImageUrlList) {

			coursewareTempVO.setName("临时课件" + i);

			coursewareTempVO.setImgUrl(tempImageUrl);

			coursewareTempVO.setType("3");

			coursewareTempService.addCoursewareTemp(coursewareTempVO);
			i++;
		}
		return new R(true);
	}

}
